import { defineStore } from 'pinia'
import { reactive, ref } from 'vue'
import { getAblumDetailApi } from '@renderer/api'

export const useAlbumStore = defineStore('useAlbumStore', () => {
    const albumList = ref([])
    const ablumDetail = reactive({} as {
        name: string,
        coverImgUrl: string,
        creator: {
            nickname: string,
            avatarUrl: string,
        },
        description: string,
    })
    const getAlbumList = async (id: number) => {
        let res = await getAblumDetailApi(id)
        if (res.code !== 200) {
            return Promise.reject(new Error('获取专辑列表失败'))
        }
         // 解构赋值
         Object.assign(ablumDetail, {
            name: res.album.name,
            coverImgUrl: res.album.blurPicUrl,
            creator: {
                nickname: res.album.artist.name,
                avatarUrl: res.album.artist.picUrl,
            },
            description: res.album.description,
        })
        albumList.value = res.songs
        albumList.value.forEach((item: any) => {
            if (!item.al.picUrl) {
                item.al.picUrl=ablumDetail.coverImgUrl
            }
        })

       
    }

    return {
        albumList,
        ablumDetail,
        getAlbumList,
    }
})